package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpRuleAddDTO;
import org.jsola.hr.dto.EmpRuleBatchAddDTO;
import org.jsola.hr.dto.SalaryCalculateRuleAddDTO;
import org.jsola.hr.dto.SalaryCalculateRuleUpdateDTO;
import org.jsola.hr.query.CalculatePayQuery;
import org.jsola.hr.query.SalaryCalculateRuleEmpInfoQuery;
import org.jsola.hr.query.SalaryCalculateRuleQuery;
import org.jsola.hr.service.ICalculatePayService;
import org.jsola.hr.service.ICalculatePayTwoService;
import org.jsola.hr.service.ISalaryCalculateRuleService;
import org.jsola.hr.vo.*;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 工价计算规则
 *
 * @author zhr
 */
@RestController("hrSalaryCalculateRuleController")
@RequestMapping(HrConstants.API_PATH + "/v1/salaryCalculateRules")
public class SalaryCalculateRuleController implements ISalaryCalculateRuleControllerAPI {

    @Autowired
    private ISalaryCalculateRuleService salaryCalculateRuleService;

    @Autowired
    private ICalculatePayService calculatePayService;

    @Autowired
    private ICalculatePayTwoService calculatePayTwoService;

    @Override
    @PostMapping
    public Result<SalaryCalculateRuleVO> save(@Valid @RequestBody SalaryCalculateRuleAddDTO salaryCalculateRuleAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增工价计算规则权限校验

        return Result.success(
                salaryCalculateRuleService.save(salaryCalculateRuleAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SalaryCalculateRuleUpdateDTO salaryCalculateRuleUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工价计算规则权限校验

        int count = salaryCalculateRuleService.update(salaryCalculateRuleUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{salaryCalculateRuleId}")
    public Result<Integer> delete(@PathVariable String salaryCalculateRuleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除工价计算规则权限校验

        int count = salaryCalculateRuleService.deleteByIds(tokenUser, salaryCalculateRuleId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] salaryCalculateRuleIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除工价计算规则权限校验

        int count = salaryCalculateRuleService.deleteByIds(tokenUser, salaryCalculateRuleIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{salaryCalculateRuleId}")
    public Result<SalaryCalculateRuleVO> findById(@PathVariable String salaryCalculateRuleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工价计算规则权限校验

        return Result.success(
            salaryCalculateRuleService.selectById(salaryCalculateRuleId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SalaryCalculateRuleListVO>> listAll(@Valid SalaryCalculateRuleQuery salaryCalculateRuleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工价计算规则权限校验

        List<SalaryCalculateRuleListVO> salaryCalculateRuleListVOList = salaryCalculateRuleService.
                select(salaryCalculateRuleQuery, tokenUser.getSiteId());
        // 需要展示使用人数则查询使用人数
        if (salaryCalculateRuleQuery.getIsShowPeopleNumber() != null && salaryCalculateRuleQuery.getIsShowPeopleNumber()) {
            salaryCalculateRuleListVOList.forEach(salaryCalculateRuleListVO -> {
                        salaryCalculateRuleService.setUsePeopleNumber(salaryCalculateRuleListVO, tokenUser);
                        salaryCalculateRuleService.setReturnValueByListVO(salaryCalculateRuleListVO);
                    });
        }
        return Result.success(
                salaryCalculateRuleListVOList
        );
    }


    @Override
    @GetMapping
    public Result<Page<SalaryCalculateRuleListVO>> page(@Valid SalaryCalculateRuleQuery salaryCalculateRuleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        Page<SalaryCalculateRuleListVO> page = salaryCalculateRuleService.selectPage(salaryCalculateRuleQuery, tokenUser.getSiteId());
        page.getEntities().forEach(salaryCalculateRuleListVO -> {
            salaryCalculateRuleService.setReturnValueByListVO(salaryCalculateRuleListVO);
        });
        return Result.success(
                page
        );
    }

    @Override
    @GetMapping("/calculatePay")
    public Result<CalculatePayVO> calculatePay(@Valid CalculatePayQuery calculatePayQuery) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                calculatePayService.calculatePay(calculatePayQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/newCalculatePay")
    public Result<CalculatePayVO> newCalculatePay(@Valid CalculatePayQuery calculatePayQuery) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                calculatePayTwoService.calculatePay(calculatePayQuery, tokenUser)
        );
    }


    @Override
    @GetMapping("/selectEmpInfoVoBySalaryCalculateRuleId")
    public Result<Page<EmpInfoVO>> selectEmpInfoVoBySalaryCalculateRuleId(@Valid SalaryCalculateRuleEmpInfoQuery salaryCalculateRuleEmpInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                salaryCalculateRuleService.selectEmpInfoVoBySalaryCalculateRuleId(salaryCalculateRuleEmpInfoQuery, tokenUser)
        );
    }


    @Override
    @GetMapping("/selectOutsourceSalary")
    public Result<Page<EmpInfoSalaryVO>> selectOutsourceSalary(@Valid SalaryCalculateRuleEmpInfoQuery salaryCalculateRuleEmpInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                salaryCalculateRuleService.selectOutsourceSalary(salaryCalculateRuleEmpInfoQuery, tokenUser)
        );
    }

    @Override
    @GetMapping("/selectRuleByEmpId")
    public Result<EmpInfoSalaryVO> selectRuleByEmpId(@RequestParam String empId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                salaryCalculateRuleService.selectRuleByEmpId(empId, tokenUser)
        );
    }

    @Override
    @PostMapping("/saveSalaryCalculateRule")
    public Result<EmpInfoSalaryVO> saveSalaryCalculateRule(@Valid @RequestBody EmpRuleAddDTO empRuleAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                salaryCalculateRuleService.saveSalaryCalculateRule(empRuleAddDTO, tokenUser)
        );
    }

    @Override
    @PostMapping("/batchSaveSalaryCalculateRule")
    public Result<Integer> batchSaveSalaryCalculateRule(@Valid @RequestBody EmpRuleBatchAddDTO empRuleBatchAddDTO) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                salaryCalculateRuleService.batchSaveSalaryCalculateRule(empRuleBatchAddDTO, tokenUser)
        );
    }


}

